#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# @author: zhangbo

# @time: 2025/3/22 16:34

from utils.tools import get_project_name, sep
from utils.read_yaml import Read_yaml
import chardet


class UpdateConfFile:

    def update_conf_file(self, source_file_path=None, param_list: list = None):
        """
        更新conf文件参数内容
        :param source_file_path:
        :param param_list:
        :return:
        """

        # 获取文件对应的编码格式
        with open(source_file_path, "rb") as f:
            rawdata = f.read()
            encoding = chardet.detect(rawdata)["encoding"]

        # 读取源文件以及缓存文件
        with open(source_file_path, "r+", encoding=encoding) as conf:
            lines = conf.readlines()
            # 定位服务器ip 并修改
            lines[4] = "    <serverIP>" + str(param_list[2]) + "</serverIP>\n"
            # 定位服务器端口 并修改
            lines[5] = "    <serverPort>" + str(param_list[3]) + "</serverPort>\n"
            # 定位终端数量
            lines[18] = "    <terminalNum>" + str(param_list[1]) + "</terminalNum>\n"
            # 定位上报gps信息 并修改开启
            lines[44] = "    <fullGps>" + str(param_list[4]) + "</fullGps>\n"
            # 定位手机号 并修改
            lines[49] = "	<phoneNumber>" + str(param_list[0]) + "</phoneNumber>\n"
            # 调整光标
            conf.seek(0)
            # 清空缓存文件内容
            conf.truncate()
            # 覆盖写入
            conf.writelines(lines)

    def update_gps_info(self, source_path=None, update_path=None):
        """
        配置gps_info文件信息
        :param source_path: 配置的源文件
        :update_path: 目标文件
        :return:
        """

        if source_path:

            # 获取文件对应的编码格式
            with open(source_path, "rb") as f, open(update_path, "rb") as g:
                rawdata = f.read()
                encoding = chardet.detect(rawdata)["encoding"]
                rawdata2 = g.read()
                encoding2 = chardet.detect(rawdata2)["encoding"]

            with (open(source_path, "r", encoding=encoding) as page_csv,
                  open(update_path, "w", encoding=encoding2) as gps_info):

                # 读取配置csv 的内容
                setting_csv = page_csv.read()

                # 清空gps_info 文件
                gps_info.truncate()

                # 将读取的内容写入到gps_info 中
                gps_info.write(setting_csv)
        else:
            pass




